package com.gs.wzmanager.dao;

import com.gs.wzmanager.dao.mapper.RoleMapper;
import com.gs.wzmanager.entity.Resource;
import com.gs.wzmanager.entity.Role;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RoleDao {

    private Logger logger = LoggerFactory.getLogger(RoleDao.class);
    @Autowired
    private RoleMapper mapper;

    //新增角色
    public void addRole(Role role) {
        try {
            mapper.addRole(role);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }

    //新增角色资源
    public void addRoleResource(int roleId, int resId) {
        try {
            mapper.addRoleResource(roleId, resId);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }

    //更新角色
    public void updateRole(Role role) {
        try {
            mapper.updateRole(role);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }

    //获取角色列表
    public List<Role> queryAllRole() {
        try {
            return mapper.queryAllRole();
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    //根据角色id获取他可以访问的全部资源
    public List<Resource> queryResourceByRoleId(int roleId) {
        try {
            return mapper.queryResourceByRoleId(roleId);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    public Role queryRoleById(int id) {
        try {
            return mapper.queryRoleById(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    //删除角色
    public void deleteRole(int id) {
        try {
            mapper.deleteRole(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }

    //删除角色资源记录
    public void deleteRoleResource(int roleId, int resId) {
        try {
            mapper.deleteRoleResource(roleId, resId);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }

}
